<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
use App\Model\Backstage\Achievement;
use Illuminate\Http\Request;
use App\Model\Account\SystemType;
use App\Http\Controllers\Backstage\AreaController;

/**
 * Class RoomWordbook
 * @package App\Model\Backstage
 * 楼盘字典model
 */
class RoomWordbook extends Base
{
    //楼盘字段数据表
    protected  $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = 'room_wordbook_'.$this->branch_id;  //后期分表
//        $this->table = 'room_wordbook';
    }

    //地铁数组
    public static $metro_arr = ['1号线','2号线','3号线','4号线','5号线','6号线','7号线','8号线'];

    //环线数据
    public static $ring_arr = ['1环内', '1环-2环', '2环-3环', '3环-4环','4环-5环','5环-6环','6环外'];

    //结构
    public static $structure_arr = ['框架', '框剪', '砖混', '钢构'];

    //土地
    public static $land_arr = [ '一级',  '二级', '三级', '四级',  '五级', '六级', '七级', '八级'];

    //外墙
    public static $wall_arr = ['墙砖', '瓷砖',  '涂料', '砂浆', '石米'];

    /**
     * @param $param
     * @return mixed
     * 添加楼盘子典
     */
    public function insertWordbook($param){
        $id = DB::table($this->table)->where('w_name', $param['w_name'])->where('w_is_del', '<', 2)->value('w_id');
        if ($id) {
            return -1;
        }
        $param['w_created_at'] = time();
        $param['w_updated_at'] = time();
        $id =  DB::table($this->table)->insertGetId($param);
        $log = new Log();
        $content = '添加楼盘字典： '.$param['w_name'];
        $log->addlog($this->store_id,$this->uid,9, $content);//员工 部门 操作类型 操作内容
        return $id;
    }

    /**
     * @param $id
     * @return mixed
     * 删除楼盘子典 此处做软删除
     */
    public function delWordbook($id){
        $oldStatus = DB::table($this->table)->where(['w_id' => $id])->value('w_is_del');
        if($oldStatus == 1){
            //删除操作
            $result =  DB::table($this->table)->where(['w_id' => $id])->update([
                'w_is_del'     => 0,
                'w_updated_at' => time(),
                'w_del_user'   => $this->uid,
                'w_del_department' => $this->store_id
            ]);
            $w_name = DB::table($this->table)->where('w_id',$id)->value('w_name');
            $content = '删除楼盘字典：'.$w_name;
            $log = new Log();
            $log->addlog($this->store_id, $this->uid, 9, $content);
            return $result;
        }elseif($oldStatus == 0){
            //恢复操作
            $result =  DB::table($this->table)->where(['w_id' => $id])->update([
                'w_is_del'     => 1,
                'w_updated_at' => time(),
                'w_del_user'   => $this->uid,
                'w_del_department' => $this->store_id
            ]);
            $w_name = DB::table($this->table)->where('w_id',$id)->value('w_name');
            $content = '恢复楼盘字典：'.$w_name;
            $log = new Log();
            $log->addlog($this->store_id, $this->uid, 9, $content);
            return $result;
        }else{
            return -1;
        }

    }

    //回收站楼盘字典列表
    // 区县 楼盘字典名称 楼盘地址 用途 类型 删除时间 删除人 删除人部门
    public function recycleBinList($pageLimit,$where,$keywords){
        if($keywords == ''){
            $list = DB::table($this->table)
                    ->where('w_is_del',0)
                    ->where($where)
                    ->select('w_id','w_name','w_resume_address','w_purpose_id',
                    'w_type_id',DB::raw('from_unixtime(w_updated_at) as w_updated_at'),'w_del_user','w_del_department')
                    ->orderBy('w_updated_at', 'desc')
                    ->paginate($pageLimit);
        }else{
            $list = DB::table($this->table)
                ->where($where)
                ->where(function ($query) use($where, $keywords){
                    $query->where('w_is_del',0)->where('w_name', 'like', '%'.$keywords.'%');//楼盘字典名称
                })
                ->select('w_id','w_name','w_resume_address','w_purpose_id',
                    'w_type_id',DB::raw('from_unixtime(w_updated_at) as w_updated_at'),'w_del_user','w_del_department')
                ->orderBy('w_updated_at', 'desc')
                ->paginate($pageLimit);
        }
        if (sizeof($list) == 0) {
            return false;
        }
            foreach($list as $key=>$value){
                $value->w_purpose_id = SystemType::getTypeDetail($value->w_purpose_id);
                $value->w_type_id = SystemType::getTypeDetail($value->w_type_id);
                $delInfo = DB::table('user')
                    ->leftjoin('department','id','u_b_id')
                    ->where('u_id',$value->w_del_user)
                    ->select('u_username','name')
                    ->first();
                $value->w_del_user = $delInfo->u_username;
                $value->w_del_department = $delInfo->name;
            }
        return $list;
    }

    /**
     * @param $param
     * @return mixed
     * 修改楼盘字典
     */
    public function saveWordbook($param){
        $param['w_updated_at'] = time();
        $oldname = DB::table($this->table)->where('w_id', $param['w_id'])->value('w_name');
        $result =  DB::table($this->table)->where('w_id' , $param['w_id'])->update($param);
        if ($oldname != $param['w_name']) {
            $content = '修改楼盘字典：'.$oldname.'=>'.$param['w_name'];
            $log = new Log();
            $log->addlog($this->store_id,$this->uid,9,$content);
        }
        return $result;
    }

    /**
     * @param $pageLimit
     * @param array $where
     * @param $keywords
     * @return mixed
     * 查询楼盘字典列表数据
     */
    public function selectWordbook($pageLimit, $where = [], $keywords){

        $list = DB::table($this->table)
            ->where(function ($query) use($where, $keywords){
                $query->where($where)->where('w_name', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords) {
                $query->where($where)->where('w_spell', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords){
                $query->where($where)->where('w_owner', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords){
                $query->where($where)->where('w_owner_phone', 'like', '%'.$keywords.'%');
            })
            ->orderBy('w_id', 'desc')
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    /**
     * @param $map
     * @return mixed
     * 获取添加房源信息时楼盘字典数据
     */
    public function getRoomWordbook($where, $keywords){
        return  DB::table($this->table)
            ->where(function ($query) use($where, $keywords){
                $query->where($where)->where('w_spell', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords) {
                $query->where($where)->where('w_name', 'like', '%'.$keywords.'%');
            })
            ->select('w_id', 'w_name as value', 'w_building_house', 'w_purpose_id', 'w_type_id')
            ->get();
    }

    //添加房源的时候用到的方法根据楼盘字典名称获取楼盘ID
    public function getRoomId($wName)
    {
        return DB::table($this->table)->where('w_name', $wName)->where('w_is_del', 1)->value('w_id');
    }

    /**
     * @param $id
     * @return mixed
     * 获取楼盘字典信息
     */
    public function getWordbook($id){
        return DB::table($this->table)
            ->where('w_id',$id)
            ->value('w_name');
    }

    /** 根据楼盘字典的ID  删除回收站的楼盘字典
     * @param $id   楼盘字典的ID
     */
    public function delRecycleBook($id)
    {
        $data = DB::table($this->table)->where('w_id', $id)->select('w_is_del', 'w_name')->first();
        if ($data->w_is_del != 0) {
            return -1;
        }
        $content = $this->name.'回收站删除：'.$data->w_name;
        $log = new Log();
        $log->addlog($this->store_id,$this->uid,9,$content);

        return DB::table($this->table)->where('w_id', $id)->update([
            'w_is_del'         =>  2,
            'w_updated_at'     => time(),
            'w_del_user'       => $this->uid,
            'w_del_department' => $this->store_id
        ]);
    }




}